package com.yff.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {
	static String driver ="com.mysql.jdbc.Driver";
	static String username="root";
	static String password="";
	static String url="jdbc:mysql://127.0.0.1:3306/guestbook?useUnicode=yes&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true";
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	public static Connection getConnection() {
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void close(Connection conn, Statement st,ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if(st!=null) {
					try {
						st.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}finally {
						if(conn!=null) {
							try {
								conn.close();
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		}
	}
	public static int updateBySql(String sql) {
		Connection conn=null;
		Statement st = null;
		conn=DBUtils.getConnection();
		int set=0;
		try {
			st = conn.createStatement();
			set=st.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return set;
	}
	public static void main(String[] args) {
		Connection conn=DBUtils.getConnection();
		System.out.println(conn);
	}
}

